<template>
  <div class="xtx-goods-page">
    <!-- 数据渲染获取的深度达到2层及以上 需要判断 避免渲染报错 -->
    <div class="container" v-if="goodDetail.categories">
      <!-- 面包屑 -->
      <XtxBread separator="/">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodDetail.categories[1].id}`">{{
          goodDetail.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goodDetail.categories[0].id}`">{{
          goodDetail.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem>{{ goodDetail.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区域 -->
        <div class="media">
          <GoodsImage :imageList="goodDetail.mainPictures" />
          <GoodsSales :goodDetail="goodDetail"/>
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <GoodsName :goodDetail="goodDetail"/>
          <!-- sku -->
          <GoodsSku :goods="goodDetail" @change="getSku"/>
          <!-- Numbox -->
          <!-- v-model 语法糖=> :modelValue + @update:modelValue -->
          <!-- <XtxNumbox
            :modelValue="buyNum"
            @update:modelValue="cg"
            :abc="1"
            :max="goodDetail.inventory"
          /> -->
          <XtxNumbox :max="goodDetail.inventory" v-model="buyNum"/>
          <!-- 加入购物车 -->
          <XtxButton @click="addCart" type="primary" style="margin-top: 10px;">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <!-- <div class="goods-tabs"></div> -->
          <GoodsTabs />
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1" />
          <GoodsHot :type="2" />
          <GoodsHot :type="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoods } from '@/api/goods'
import { ref, onMounted, provide } from 'vue'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image'
import GoodsSales from './components/goods-sales'
import GoodsName from './components/goods-name'
import GoodsTabs from './components/goods-tabs'
import GoodsHot from './components/goods-hot'
import { useStore } from 'vuex'
import { message } from '@/components/Message'
export default {
  name: 'XtxGoodsPage',

  components: {
    GoodsImage,
    GoodsSales,
    GoodsName,
    GoodsTabs,
    GoodsHot
  },

  setup () {
    const goodDetail = ref({})

    // 购买的数据
    const buyNum = ref(1)

    // 导入store
    const store = useStore()

    // const cg = (data) => {
    //   console.log(data)
    //   buyNum.value = data
    // }

    // 依赖注入全局数据
    provide('goods', goodDetail)

    const route = useRoute()
    const getGoodDetail = async () => {
      const { result } = await findGoods(route.params.id)
      // console.log(result)
      goodDetail.value = result
    }

    // 获取sku的值
    // sel sku（销售属性）选择完之后最新的价格和库存
    // 当前渲染sku规格的商品核心信息
    const currSku = ref(null)

    const getSku = (sel) => {
      // console.log(sel)
      // 更新选择完 sku 的商品价格和库存数量
      if (sel.skuId) {
        goodDetail.value.price = sel.price
        goodDetail.value.oldPrice = sel.oldPrice
        goodDetail.value.inventory = sel.inventory
        // 渲染完整存一次
        currSku.value = sel
      } else {
        // 清除上次的选择
        currSku.value = null
      }
    }

    // 点击加入购物车
    const addCart = async () => {
      // console.log(currSku.value)
      //       说明：
      // 1. 存储当前选择完规格生成的sku商品数据
      // 2. 没有选择商品规格或库存不足不能添加
      // 3. 调用action传入当前商品必要数据存到vuex
      if (!currSku.value) {
        return message({ type: 'warn', text: '请选择购买商品的完整属性！' })
      }
      if (currSku.value.inventory === 0) {
        return message({ type: 'warn', text: '没有库存了！' })
      }

      // 可以加入购物车了
      await store.dispatch('cart/addCartAction', {
        id: goodDetail.value.id, // 商品ID
        name: goodDetail.value.name, // 商品名字
        picture: goodDetail.value.mainPictures[0], // 商品配图
        skuId: currSku.value.skuId, // sku ID
        price: currSku.value.oldPrice,
        nowPrice: currSku.value.price, // 选择完sku的商品价格
        attrsText: currSku.value.specsText,
        stock: currSku.value.inventory, // 选择完sku的商品库存
        selected: true, // 商品的选中状态
        isEffective: true, // 商品是否是有效的（可能加入之后下架了/没库存了）
        count: buyNum.value // 购买数量
      })
      message({ type: 'success', text: '加入购物车成功！' })
    }

    onMounted(() => {
      getGoodDetail()
    })

    return { goodDetail, getSku, buyNum, addCart }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
}
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  // 新增样式
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
    background: #fff;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
